﻿import json;
import requests;
import time;
import mysql.connector;
import re
from tqdm import tqdm
import pymongo
import datetime


class bg:

    def __init__(self, Cookie):

        self.Cookie = Cookie

        self.removeName = ['张献伟', '张婷', '闵雨虹', '贾萌', '魏爽', '倪婷雨', '邢洋', '刘茂清', '侯亚红']


    def getToke(name_i):

        mydb = mysql.connector.connect(
            host="localhost",  # 数据库主机地址
            user="root",  # 数据库用户名
            passwd="root",  # 数据库密码
            database="weizandata"
        )
        mycursor = mydb.cursor()
        sql = "SELECT toke FROM toke WHERE name_i ='" + name_i + "';"
        mycursor.execute(sql)
        myresult = mycursor.fetchall()  # fetchall() 获取所有记录
        mydb.close()
        return myresult[0][0]

    def addData(self, ssql):

        mydb = mysql.connector.connect(
            host="localhost",  # 数据库主机地址
            user="root",  # 数据库用户名
            passwd="root",  # 数据库密码
            database="linxidb"
        )
        cmd = mydb.cursor()
        cmd.execute(ssql)
        mydb.commit()
        rowcount = cmd.rowcount
        mydb.close()
    def selectCustomerStatus(self,customerId):
        mydb = mysql.connector.connect(
            host="localhost",  # 数据库主机地址
            user="root",  # 数据库用户名
            passwd="root",  # 数据库密码
            database="linxidb"
        )
        cmd = mydb.cursor()
        cmd.execute("SELECT userLevel FROM customers WHERE customerId='{}'".format(customerId))
        myresult = cmd.fetchall()
        mydb.close()
        if len(myresult)==0:
            return 300
        else:
            return myresult[0][0]



    def getD(self, sustomerId):  # 获取电话的备注

        url = 'https://crm.byai.com/api/customer/detail?customerId=' + str(sustomerId)

        header = {'Cookie': self.Cookie};

        r = requests.get(url=url, headers=header);

        return json.loads(r.text)

    def getRgList(self, min=60):  # 获取人工外呼的呼出记录

        startDate = (datetime.datetime.now() + datetime.timedelta(minutes=-min)).strftime("%Y-%m-%d %H:%M:%S")
        endDate = time.strftime("%Y-%m-%d+%H:%M:%S")
        # startDate = '2021-03-17 00:00:00'
        # endDate = '2021-03-17 23:59:59'
        pageNum = 1
        pageSize = 30000
        print("{}-{}".format(startDate, endDate))

        url = 'https://aicrm.byai.com/api/artificial/getCsCallList?customerStatus=&searchContext=&status=&callInstanceId=&customerTelephone=&startDate={}&endDate={}&csStaffIdList=&pageNum={}&pageSize={}&total=40'.format(
            startDate, endDate, pageNum, pageSize)

        header = {'Cookie': self.Cookie};

        r = requests.get(url=url, headers=header);

        datalist = json.loads(r.text)

        pages = datalist['data']['pages']
        print(pages)
        print('total', datalist['data']['total'])
        dataresList = datalist['data']['list']
        print(len(dataresList))
        if pages > 1:
            for i in range(2, int(pages) + 1):
                url = 'https://aicrm.byai.com/api/artificial/getCsCallList?customerStatus=&searchContext=&status=0&callInstanceId=&customerTelephone=&startDate={}&endDate={}&csStaffIdList=&pageNum={}&pageSize={}&total=40'.format(
                    startDate, endDate, i, pageSize)
                r = requests.get(url=url, headers=header)
                datalist = json.loads(r.text)['data']['list']
                print(len(datalist))
                dataresList.extend(datalist)

        return dataresList



    def sleeptime(self, hour, min, sec):

        return hour * 3600 + min * 60 + sec;



    def byCaozuoShouDong(self, min):  # 人工拨打A客资操作
        getRgList = b.getRgList(min)
        if(len(getRgList)==0):
            return
        customerIds = []
        for x in getRgList:
            customerIds.append(x['customerId'])
        print('去重后的', len(list(set(customerIds))))
        setCustomerIds = list(set(customerIds))
        for i in tqdm(range(0, len(setCustomerIds))):  # 11134
            customerId = setCustomerIds[i]
            customerLevel = b.selectCustomerStatus(customerId)
            if customerLevel != 2:
                resdict = b.getD(customerId)['data']
                delkeys = ["companyId", "name", "defaultContactId", "lastJobId", "lastJobName", "lastContractTime",
                           "predictDealDate", "phoneStatus", "lockStatus", "receiveTime", "duration", "labelInfoIds",
                           "original", "businessCardId", "customerCompanyName", "avatarUrl", "customerExist",
                           "inBlackList",
                           "customerProperties"]
                for dekey in delkeys:
                    del resdict[dekey]

                filed = "(" + ",".join(list(resdict.keys())) + ")"

                valStr = ''
                for item in list(resdict.values()):
                    if item == None:
                        valStr += "," + 'null'
                    else:
                        valStr += ",'" + str(item) + "'"
                valStr = "(" + valStr[1:] + ")"
                sqll = 'REPLACE INTO customers ' + filed + ' VALUE ' + valStr

                self.addData(sqll)


if __name__ == '__main__':

    cookie = bg.getToke('bjrg')
    b = bg(cookie)
    min = int(input("请输入爬去间隔分钟数："))
    b.byCaozuoShouDong(min)
    second = b.sleeptime(0, 0, 5);
    n=1
    while (1 == 1):
        try:
            xiuxi = int(time.strftime("%H"))
            if (xiuxi >= 8 and xiuxi <= 12) or (xiuxi >= 13 and xiuxi < 20):
                print("第{}次请求".format(n))
                n = n + 1
                cookie = bg.getToke('bjrg')
                b = bg(cookie)
                b.byCaozuoShouDong(min)
                time.sleep(second)
        except:
            print('error')
